package com.bysj.studentsystem.service.impl;

import com.bysj.studentsystem.entity.SysMajor;
import com.bysj.studentsystem.dao.SysMajorDao;
import com.bysj.studentsystem.service.SysMajorService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (SysMajor)表服务实现类
 *
 * @author makejava
 * @since 2024-03-09 16:51:49
 */
@Service("sysMajorService")
public class SysMajorServiceImpl implements SysMajorService {
    @Resource
    private SysMajorDao sysMajorDao;

    @Autowired
    private UserDetailsServiceImpl userDetailsService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysMajor queryById(Integer id) {
        return this.sysMajorDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param sysMajor 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo<SysMajor> queryByPage(SysMajor sysMajor, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysMajor> list = this.sysMajorDao.queryList(sysMajor);
        return new PageInfo(list);
    }

    /**
     * 新增数据
     *
     * @param sysMajor 实例对象
     * @return 实例对象
     */
    @Override
    public SysMajor insert(SysMajor sysMajor) {
        sysMajor.setCreateBy(userDetailsService.getCurrentUserId());
        sysMajor.setCreateTime(new Date());
        this.sysMajorDao.insert(sysMajor);
        return sysMajor;
    }

    /**
     * 修改数据
     *
     * @param sysMajor 实例对象
     * @return 实例对象
     */
    @Override
    public SysMajor update(SysMajor sysMajor) {
        sysMajor.setUpdateBy(userDetailsService.getCurrentUserId());
        sysMajor.setUpdateTime(new Date());
        this.sysMajorDao.update(sysMajor);
        return this.queryById(sysMajor.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.sysMajorDao.deleteById(id) > 0;
    }
}
